package models

import (
	"behavior_srv/basic/config"
	"time"
)

// StayTime 用户停留时间
type BehaviorStayTimes struct {
	Id         uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	UserId     int64     `gorm:"column:user_id;type:bigint;comment:用户ID;default:0;" json:"user_id"`              // 用户ID
	SessionId  string    `gorm:"column:session_id;type:varchar(100);comment:会话ID;not null;" json:"session_id"`   // 会话ID
	Path       string    `gorm:"column:path;type:varchar(255);comment:页面路径;not null;" json:"path"`               // 页面路径
	PageName   string    `gorm:"column:page_name;type:varchar(100);comment:页面名称;" json:"page_name"`              // 页面名称
	StayTimeMs int64     `gorm:"column:stay_time_ms;type:bigint;comment:停留时间(毫秒);not null;" json:"stay_time_ms"` // 停留时间(毫秒)
	EnterTime  time.Time `gorm:"column:enter_time;type:datetime(3);comment:进入时间;not null;" json:"enter_time"`    // 进入时间
	LeaveTime  time.Time `gorm:"column:leave_time;type:datetime(3);comment:离开时间;not null;" json:"leave_time"`    // 离开时间
	CreatedAt  time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

// TableName 设置表名
func (s *BehaviorStayTimes) TableName() string {
	return "behavior_stay_times"
}

// Create 创建停留时间记录
func (s *BehaviorStayTimes) Create() error {
	return config.DB.Create(s).Error
}

// BatchCreate 批量创建停留时间记录
func (s *BehaviorStayTimes) BatchCreate(times []*BehaviorStayTimes) error {
	return config.DB.CreateInBatches(times, 100).Error
}
